package com.example.drainage.ui.certupload.dataview

import android.annotation.SuppressLint
import android.text.Editable
import android.text.InputFilter
import android.text.TextWatcher
import android.widget.EditText
import android.widget.TextView

/**
 * 输入框+展示输入数量
 *
 * @author 创建人 ：yanghaozhang
 * @version 1.0
 * @package 包名 ：com.augurit.agmobile.agwater5.drainage.complex.emergency.dangerous.view.dataview
 * @createTime 创建时间 ：2021/10/31
 * @modifyBy 修改人 ：
 * @modifyTime 修改时间 ：
 * @modifyMemo 修改备注：
 */
@SuppressLint("SetTextI18n")
class EditWithCountDataSpot(outputKey: String, editView: EditText, private val countTextView: TextView, title: String, private val textLimitLength:Int = 50) :
    EditNormalDataSpot(outputKey, editView, title) {

    var maxLength: Int = if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
        editView.filters.firstOrNull { filter ->
            filter is InputFilter.LengthFilter
        }?.let { lengthFilter ->
            lengthFilter as InputFilter.LengthFilter
        }?.max ?: textLimitLength
    } else {
        textLimitLength
    }

    init {
        countTextView.text = "0/${maxLength}"
        editView.addTextChangedListener(object :TextWatcher{
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

            }

            override fun afterTextChanged(s: Editable?) {
                val inputStr = editView.text.toString()
                countTextView.text = "${inputStr.length}/${maxLength}"
            }

        })
    }
}